//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    imgurl:app.globalData.imgurl,
    hiddenClear:false,
    keyword:'',
    navlist: [
      [{
        imgurl: '/images/nav_icon1.png',
        text: '租赁'
      }, {
        imgurl: '/images/nav_icon2.png',
        text: '便民'
      }, {
          imgurl: '/images/nav_icon3.png',
        text: '店铺'
      }, {
        imgurl: '/images/nav_icon4.png',
        text: '分类'
      }]
    ],
    banners: [
      {
        imgurl: '/images/banner_img.png'
      }
    ],
    hiddenClear: true
  },
  // 点击导航按钮
  switchNavTab: function (e) {

  },
  //事件处理函数
  bindViewTap: function () {

  },
  // 获取搜索关键字
  getvalue: function (e) {
    var keyword = e.detail.value;
    this.setData({
      hiddenClear: keyword.length > 0 ? true : false ,
      keyword: keyword
    })
    if (keyword == ''){
      this.getGoodsList()
      return      
    }
    this.getGoodsList2(keyword)
  },
  // 清空关键字
  clearKeyword: function () {
    this.setData({
      hiddenClear: true,
      keyword: ''
    })
    this.getGoodsList()
  },
  onLoad: function () {
    this.getnavBg()
    this.getBanners()
    this.getGoodsList()
  },
  // 顶部背景
  getnavBg: function () {
    var that = this
    wx.request({
      url: app.globalData.url + 'banner',
      success: res => {
        console.log(res)
        var img = res.data[0].img
        that.setData({
          navBg: img
        })
      }
    })
  },
  // 获取导航信息
  getNavs:function(){
    wx.request({
      url: app.globalData.url + 'banner',
      success: res => {
        var img = res.data[0].img
        that.setData({
          navBg: img
        })
      }
    })
  },
  // 轮播图
  getBanners:function(){
    var that = this
    wx.request({
      url: app.globalData.url + 'index_banner',
      success: res => {
        var bannerlist = res.data
        that.setData({
          bannerlist: bannerlist
        })
      }
    })
  },
  // 商品列表
  getGoodsList2: function (keyword) {
    var that = this
    wx.request({
      url: app.globalData.url + 'sousuo?title='+keyword,
      success: res => {
        var goodlist = res.data
        for (var i = 0; i < goodlist.length; i++) {
          if (goodlist[i].title.length > 32) {
            goodlist[i].title_str = goodlist[i].title.substr(0, 32)
          } else {
            goodlist[i].title_str = goodlist[i].title
          }
        }
        that.setData({
          goodlist: goodlist
        })
      }
    })
  },
  // 商品列表
  getGoodsList:function(){
    var that = this
    wx.request({
      url: app.globalData.url + 'index',
      success: res => {
        var goodlist = res.data
        for (var i = 0; i < goodlist.length; i++){
          if (goodlist[i].title.length > 32) {
            goodlist[i].title_str = goodlist[i].title.substr(0, 32)
          } else {
            goodlist[i].title_str = goodlist[i].title
          }
        }
        that.setData({
          goodlist: goodlist
        })
      }
    })
  },
  switchNavTab:function(e){
    var that = this
    var o = e.currentTarget.dataset
    var url = '';
    switch(o.index){
      case 0:
        wx.navigateTo({
          url: '/pages/list/zulin',
        })
        break;
      case 1:
        wx.navigateTo({
          url: '/pages/list/bianmin',
        })
        break;
      case 2:
        wx.switchTab({
          url: '/pages/list/shoplist',
        })
        break;
      case 3:
        wx.navigateTo({
          url: '/pages/list/zulin',
        })
        break;
    }
  },
  // 打开商品详情
  toGoodDetail:function(e){
    var o = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/list/detail/detail?id=' + o.id,
    })
  },
  // 打开店铺
  toShop:function(e){
    var o = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/list/shop?shopid=' + o.id,
    })
  },
  getUserInfo: function (e) {

  }
})
